import APIConfig from "../config/API";
import apiController from "./api";
import { auth, authCheck } from "../service/auth";

export default function controller(router){
    apiController("users", router);
    apiController("posts", router);
    apiController("movies", router);
    apiController("cars", router);
    apiController("commands", router);
    apiController("reviews", router);
    apiController("customers", router);
    return router
    .get('/', async (ctx) => {
        ctx.renderHTML('index.html');
    })
    .get('/register', async (ctx)=>{
        ctx.renderHTML('register.html');
    })
    .get('/admin', async (ctx)=>{
        ctx.renderHTML('admin.html');
    })
    .get(APIConfig.BASIC_PATH, async (ctx)=>{
        ctx.rest({
            msg: "API server test success",
            code: "test:success"
        })
    })
    .post(APIConfig.AUTH_PATH, async ctx => {
        const { username, password} = ctx.request.body;
        const token = await auth(username, password);
        ctx.rest({
            token: token,
            code: "auth:success"
        })
        
    })
    .post(APIConfig.AUTH_PATH+"/check", async ctx => {
        const { token } = ctx.request.body;
        const decode = await authCheck(token);
        if(decode){
            ctx.rest({
                message: 1,
                code: "auth:check:success"
            })
        }
        ctx.rest({
            message: 0,
            code: "auth:check:success"
        })
        
        
    })
    .get(APIConfig.BASIC_PATH+"/ipfs", async ctx => {
     
    })
    .get(APIConfig.BASIC_PATH+"/ipfs/demo", async ctx => {
       
    })
    .post(APIConfig.BASIC_PATH+"/ipfs", async ctx=>{
        
    })
    .get("/fileupload", async ctx => {
        ctx.renderHTML('/fileupload.html');
    })
    .get("/player", async ctx => {
        ctx.renderHTML('/player.html');
    })
    .get("/install", async ctx=> {
        ctx.renderHTML('/install.html')
    })
    .get("*", async ctx => {
        ctx.renderHTML('/404.html');
    })
}